package com.example.spring_mvc.listener;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;

public class ContextLoaderListener implements ServletContextListener {

    public void contextInitialized(ServletContextEvent servletContextEvent){
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("application.xml");
        //将Spring应用上下文对象存储到ServletContext域中
        ServletContext servletContext = servletContextEvent.getServletContext();
        servletContext.setAttribute("application",applicationContext);
        System.out.println("Spring容器创建完毕");
    }
    public void contextDestroyed(ServletContextEvent servletContextEvent){

    }
}
